#!/usr/bin/python3
# -*- coding:UTF-8 -*-

import argparse
import os
import time
import traceback
import json
import AutoExecUtils
import TagentClient


def usage():
    pname = os.path.basename(__file__)
    print("{} --node <node> --timeout <timeout seconds> .\n".format(pname))
    exit(-1)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--node', default='', help='Execution node json')
    parser.add_argument('--timeout', default='', help='Output json file path for node')

    args = parser.parse_args()
    timeout = args.timeout
    node = args.node

    try:
        nodeInfo = {}
        hasOptError = False
        if node is None or node == '':
            node = os.getenv('AUTOEXEC_NODE')
        if node is None or node == '':
            print("ERROR: Can not find node definition.\n")
            hasOptError = True
        else:
            nodeInfo = json.loads(node)

        if hasOptError:
            usage()
            exit(1)

        if timeout == '':
            timeout = 10
        else:
            timeout = int(timeout)

        errMsg = None
        print("INFO: Try to connect to Tagent {}:{}.\n".format(nodeInfo['host'], nodeInfo['protocolPort']))
        startTime = time.time()
        tagent = TagentClient.TagentClient(nodeInfo['host'], nodeInfo['protocolPort'], nodeInfo['password'], connectTimeout=timeout, readTimeout=timeout, writeTimeout=timeout)

        port = nodeInfo.get('port')
        if port is not None:
            port = int(port)

        data = {'MGMT_IP': nodeInfo.get('host'),
                'PORT': port,
                'RESOURCE_ID': nodeInfo.get('resourceId'),
                'AVAILABILITY': 0
                }

        try:
            tagent.echo(None, "Agent logon success.\n", 1)
            timeConsume = round(time.time() - startTime, 4)
            data['AVAILABILITY'] = 1
            data['ERROR_MESSAGE'] = ''
            data['RESPONSE_TIME'] = timeConsume
            print("FINE: Tagent logon succeed.\n")
        except Exception as ex:
            timeConsume = round(time.time() - startTime, 4)
            errMsg = str(ex)
            data['AVAILABILITY'] = 0
            data['ERROR_MESSAGE'] = errMsg
            data['RESPONSE_TIME'] = timeConsume
            print("WARN: Tagent logon failed.\n")

        out = {'DATA': data}
        AutoExecUtils.saveOutput(out)
    except Exception as ex:
        print('ERROR: Unknow Error, {}'.format(traceback.format_exc()))
        exit(-1)
